package leetcode.code0075;

public class Solution extends Solution75 {

	@Override
	public void sortColors(int[] nums) {
		int p0 = 0;// 指向最后一个0后面一位, 可用位置
		int p2 = nums.length - 1;// 指向后面所有2的最前面一位, 可用位置
		int p1 = 0; // 指向当前位置, 找1的针
		while (p1 <= p2) {
			int num = nums[p1];
			if (num == 2) {
				if (p1 != p2) {
					swap(nums, p1, p2);
					p2--;
				} else {
					break;
				}
			} else if (num == 0) {
				if (p1 != p0) {
					swap(nums, p1, p0);
					p0++;
				} else {
					p0++;
					p1++;
				}
			} else if (num == 1) {
				p1++;
			}
		}
	}

	private void swap(int[] nums, int a, int b) {
		int temp = nums[a];
		nums[a] = nums[b];
		nums[b] = temp;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
